package practice.w902;

import java.util.Arrays;
import java.util.Random;

public class ArrayDemo {
    public static void main(String[] args) {
//        int[] a = new int[10];
//        //3种定义
//        int[] b = new int[5];
//        int[] c = {1,2,3,5,4};
//        int[] d =new int[]{1,2,3,4,5};
//
//
//        int[] arr1 = new int[10];
//        System.out.println(arr1.length);//输出数组长度
//        Random r = new Random();
//        for (int i = 0; i < arr1.length; i++) {
//            arr1[i] = r.nextInt(10);//元素赋值
//        }
//        System.out.println(arr1[arr1.length-1]);//输出最后一个元素
//
//
//        //定义数组，包含10个元素，遍历数组，给每个元素赋值为0到99之间的随机数，输出每个元素的值
//        int[] arr2 = new int[10];
//        for (int i = 0; i < arr2.length; i++) {
//            arr2[i] = (int)(Math.random()*100);
//            System.out.println(arr2[i]);
//        }
//
//
//        //复制方法1
//        int[] arr3 = {1,2,3,5,4};
//        a= Arrays.copyOf(arr3, arr3.length+1);//可以扩容
//        for (int i = 0; i < a.length; i++) {
//            System.out.println(a[i]);
//        }
//
//        //复制方法2
//        int[] arr4 = {1,2,3,5,4};
//        int[] arr5 = new int[6];
//        System.arraycopy(arr4, 0, arr5, 0, 4);//arr4源数组
//       //0：原数组起始下标    arr5：目标数组  0：目标数组起始下标  4：要复制元素个数
//        for (int i = 0; i < arr5.length; i++) {
//            System.out.println(arr5[i]);
//        }
//
//        //复制方法3
//        int[] arr6 = {1,2,3,5,4};
//        int[] arr7 = Arrays.copyOf(arr6, arr6.length+1);
//        for (int i = 0; i <arr7.length ; i++) {
//            System.out.println(arr7[i]);
//
//        }
        int[] a = new int[10];

        int[] b = new int[5];
        int[] c = {1,2,3,5,4};
        int[] d =new int[]{1,2,3,4,5};


        int[] arr1 = new int[10];
        System.out.println(arr1.length);
        Random r = new Random();
        for (int i = 0; i < arr1.length; i++) {
            arr1[i] = r.nextInt(10);
        }
        System.out.println(arr1[arr1.length-1]);


        int[] arr2 = new int[10];
        for (int i = 0; i < arr2.length; i++) {
            arr2[i] = (int)(Math.random()*100);
            System.out.println(arr2[i]);
        }


        int[] arr3 = {1,2,3,5,4};
        a= Arrays.copyOf(arr3, arr3.length+1);
        for (int i = 0; i < a.length; i++) {
            System.out.println(a[i]);
        }


        int[] arr4 = {1,2,3,5,4};
        int[] arr5 = new int[6];
        System.arraycopy(arr4, 0, arr5, 0, 4);

        for (int i = 0; i < arr5.length; i++) {
            System.out.println(arr5[i]);
        }

        int[] arr6 = {1,2,3,5,4};
        int[] arr7 = Arrays.copyOf(arr6, arr6.length+1);
        for (int i = 0; i <arr7.length ; i++) {
            System.out.println(arr7[i]);

        }
    }
}
